package com.fzzz.kotlindemo

import android.app.Activity
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.Editable
import android.text.TextWatcher
import android.text.method.TransformationMethod
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast

class SecondActivity : AppCompatActivity() {
    private lateinit var etInput: EditText
    private lateinit var btn: Button
    private lateinit var tvContent: TextView
    private lateinit var btnClose: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        etInput = findViewById(R.id.et_input)
        btn = findViewById(R.id.btn)
        tvContent = findViewById(R.id.tv_content)
        btnClose = findViewById(R.id.btn_close)
        //new 对象传进去
        //kotlin没有set之类的方法，只能理解为给属性赋值
        //实在搞不定，用Java写，菜单code -> convert java file to kotlin file 参考SecondActivity1
        tvContent.transformationMethod = AllCapTransformationMethod()

        //空安全变量
        val btnString: String = "确认"
        val contentString = "输出内容"
        //可为空字符串变量
        var inputHint: String? = null
        inputHint = "输入数字"

        etInput.setHint(inputHint)
        btn.setText(btnString)
        tvContent.setText(contentString)

        val user = intent.getParcelableExtra<User>("user")
        tvContent.setText(user.name + "-----" + user.age)

        btnClose.setOnClickListener {
            setResult(Activity.RESULT_OK, intent)
            finish();
        }

        btn.setOnClickListener {
            val x = etInput.text.toString().trim()
            Toast.makeText(this, x, Toast.LENGTH_SHORT).show()
//            val i = x.toInt()
//            when (i) {
//                2 -> tvContent.setText("等于2")
//                in 3..5 -> tvContent.setText("3到5之间")
//                !in 8..9 -> tvContent.setText("不在8-9之间")
//                is Int -> tvContent.setText("类型判断")
//                else -> tvContent.setText("else")
//            }

//            when {
//                i == 2 -> tvContent?.setText("等于2")
//                i > 3 && i <= 5 -> tvContent?.setText("3到5之间")
//                else -> tvContent?.setText("else")
//            }
        }

//        btn.setOnClickListener(object :View.OnClickListener{
//            override fun onClick(v: View?) {
//
//            }
//        })

        etInput.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                println("afterTextChanged:s = " + s)
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
                println("beforeTextChanged:s = " + s)
                println("beforeTextChanged:start = " + start)
                println("beforeTextChanged:after = " + after)
                println("beforeTextChanged:count = " + count)
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                println("onTextChanged:s = " + s)
                println("onTextChanged:start = " + start)
                println("onTextChanged:before = " + before)
                println("onTextChanged:count = " + count)
            }
        })
    }

//    fun EditText.setTextChangeListener(body: (key: String) -> Unit) {
//        addTextChangedListener(object : TextWatcher {
//            override fun afterTextChanged(s: Editable?) {
//            }
//
//            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
//            }
//
//            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
//                body(s.toString())
//            }
//        })
//    }

}
